package com.itheima.cart.controller;


import com.itheima.cart.dto.ShoppingCartDTO;
import com.itheima.cart.pojo.ShoppingCart;
import com.itheima.cart.service.ShoppingCartService;
import com.itheima.result.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "C端-购物车接口")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping
   public Result<List<ShoppingCart>>  getByDishIdOrSetmealIdWithUserId(@RequestBody ShoppingCart shoppingCart){
        List<ShoppingCart> carts=shoppingCartService.getByDishIdOrSetmealIdWithUserId(shoppingCart);
        return Result.success(carts);
   }

   @DeleteMapping("/{userId}")
   public void clean(@PathVariable  Long userId){
        shoppingCartService.cleanmCart(userId);
   }
    /**
     *添加购物车
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("{}",shoppingCartDTO);
        shoppingCartService.add(shoppingCartDTO);
        return Result.success();
    }

    @PostMapping("/addCart")
    public Result add(@RequestBody ShoppingCart shoppingCart){
        log.info("{}",shoppingCart);
        shoppingCartService.addshoppingCart(shoppingCart);
        return Result.success();
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public Result list(){
        List<ShoppingCart> list=shoppingCartService.show();
        return Result.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public Result clean(){
        shoppingCartService.clean();
        return Result.success();
    }

    /**
     * 减少一
     * @param shoppingCartDTO
     * @return
     */

    @PostMapping("/sub")
    public Result reduce(@RequestBody ShoppingCartDTO shoppingCartDTO){

        shoppingCartService.reduce(shoppingCartDTO);
        return Result.success();
    }



}
